package edu.uclmgmu.bacteriosec.aspects.common;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Vector;

public class Store {
	private static final int LOG_WITH_FILES = 0;
	private static int howToLog;
	
	public static void setHowToLog(int howToLog) {
		Store.howToLog=howToLog;
	}
	
	public static void updateNumberOfCalls(String joinPointName) {
		if (Store.howToLog==Store.LOG_WITH_FILES) {
			FilesStore.updateNumberOfCalls(joinPointName);
		} else {
			RMIStore.updateNumberOfCalls(joinPointName);
		}
	}

	public static int getNumberOfCallInTurn(String joinPointName) {
		if (Store.howToLog==Store.LOG_WITH_FILES) {
			return FilesStore.getNumberOfCallInTurn(joinPointName);
		} else {
			return RMIStore.getNumberOfCallInTurn(joinPointName);
		}
	}

	public static int getNumberOfCurrentCall(String joinPointName) {
		if (Store.howToLog==Store.LOG_WITH_FILES) {
			return FilesStore.getNumberOfCurrentCall(joinPointName);
		} else {
			return RMIStore.getNumberOfCurrentCall(joinPointName);
		}
	}

	public static void updateNumberOfCurrentCall(String joinPointName) {
		if (Store.howToLog==Store.LOG_WITH_FILES) {
			FilesStore.updateNumberOfCurrentCall(joinPointName);
		} else {
			RMIStore.updateNumberOfCurrentCall(joinPointName);
		}
	}

	public static void log(String canonicalName, int numberOfCurrentCall, String signature) {
//		String text="Operator: " + canonicalName + "\nCurrent call: " + numberOfCurrentCall + "\nJoin point:" + signature + "\n\n";
//		System.out.println(text);
	}

	public static String getCurrentjoinPoint() {
		if (Store.howToLog==Store.LOG_WITH_FILES) {
			return FilesStore.getCurrentjoinPoint();
		} else {
			return RMIStore.getCurrentjoinPoint();
		}
	}

	public static int getNumberOfCallsToJP(String jpFile) {
		if (Store.howToLog==Store.LOG_WITH_FILES) {
			return FilesStore.getNumberOfCallsToJP(jpFile);
		} else {
			return RMIStore.getNumberOfCallsToJP(jpFile);
		}
	}

	public static void saveCurrentJoinPoint(String jpFile) {
		if (Store.howToLog==Store.LOG_WITH_FILES) {
			FilesStore.saveCurrentJoinPoint(jpFile);
		} else {
			RMIStore.saveCurrentJoinPoint(jpFile);
		}
	}

	public static void createFileForCallInTurn(String jpFile, int callInTurn) throws IOException {
		if (Store.howToLog==Store.LOG_WITH_FILES) {
			FilesStore.createFileForCallInTurn(jpFile, callInTurn);
		} else {
			RMIStore.createFileForCallInTurn(jpFile, callInTurn);
		}
	}

	public static void saveAOP_AJC_XML(String aopXMLPath, String text) throws IOException {
		FileOutputStream fos=new FileOutputStream(aopXMLPath);
		fos.write(text.getBytes());
		fos.close();
	}

	public static int readNumberOfCalls(String jpFile) throws IOException {
		if (Store.howToLog==Store.LOG_WITH_FILES) {
			return FilesStore.readNumberOfCalls(jpFile);
		} else {
			return RMIStore.readNumberOfCalls(jpFile);
		}
	}
	
	public static void createFileForCurrentCall(String jpFile, int currentCall) throws IOException {
		if (Store.howToLog==Store.LOG_WITH_FILES) {
			FilesStore.createFileForCurrentCall(jpFile, currentCall);
		} else {
			RMIStore.createFileForCurrentCall(jpFile, currentCall);
		}
	}

	public static Vector<String> loadFilesNumberOfCalls() {
		if (Store.howToLog==Store.LOG_WITH_FILES) {
			return FilesStore.loadFilesNumberOfCalls();
		} else {
			return RMIStore.loadFilesNumberOfCalls();
		}
	}


}
